<?php
include("mysqlcon.php");
include("userid.php");
include("ok.php");
include("error.php");
$reserv_id_raw = $_POST['reserv_id'];
$reserv_id = explode(",", $reserv_id_raw);
$reserv_gid = $_POST['reserv_gid'];
$customer_id = $_POST['customer_id'];
$customer_address = $_POST['customer_address'];
$cid = $_POST['cid'];
$room_id = array();
//$room_id = $_POST['room_id'];
//$room_id = explode(",", $room_id);
 
//$ttt = $_POST['room_list'];
//echo $ttt;

if($customer_id ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : รหัสบัตรประจำตัวประชาชน<br>";}

if($error==true) error_page($error_text);

if($error==false){
	mysql_select_db($dbname,$conn);
	mysql_query('SET NAMES utf8');


	$save_customerid_query = "UPDATE customer SET `customer_id` = '$customer_id' WHERE `id` = $cid";
	mysql_query($save_customerid_query,$conn);
	$save_customeraddress_query = "UPDATE customer SET customer_address = '$customer_address'  WHERE `id` = $cid";
	mysql_query($save_customeraddress_query,$conn);

	for($i = 0 ;$i < sizeof($reserv_id); $i++){
		$select_room_id = "SELECT room_id FROM reservation WHERE reserv_id = '".trim($reserv_id[$i])."'";
		$rs = mysql_query($select_room_id,$conn);
		$rs_room_id = mysql_fetch_array($rs);
		array_push($room_id, trim($rs_room_id['room_id']));
		
		$update_room_status = "UPDATE resort SET `room_status` = '1' WHERE `room_id` = '".trim($rs_room_id['room_id'])."'";
		mysql_query($update_room_status,$conn);
		
		$update_reserv_status = "UPDATE reservation SET reserv_status = '4' WHERE reserv_id = '".trim($reserv_id[$i])."'";
		mysql_query($update_reserv_status,$conn);
		
		//save log
		$save_log_checkin ="INSERT INTO checkin(reserv_id,userlist_id)";
		$save_log_checkin=$save_log_checkin."VALUES('".trim($reserv_id[$i])."','$userlist_id')";
		mysql_query($save_log_checkin,$conn);
	}

	/*for($i = 0 ;$i < sizeof($room_id); $i++){
		$update_room_status = "UPDATE resort SET `room_status` = '1' WHERE `room_id` = '".trim($room_id[$i])."'";
		mysql_query($update_room_status,$conn);
	}*/

	$update_customeid_reserv = "UPDATE reservation SET customer_id = '$customer_id' WHERE id = '$cid'";
	mysql_query($update_customeid_reserv,$conn);


	//save log
	/*$save_log_checkin ="INSERT INTO checkin(reserv_id,userlist_id)";
	$save_log_checkin=$save_log_checkin."VALUES('$reserv_gid','$userlist_id')";
	if(!mysql_query($save_log_checkin,$conn))
	error_page("ฐานข้อมูลผิดพลาด $strSQL");
	else
	{*/
		$ok_text = "ห้องหมายเลข : ".implode(",", $room_id)."<br>";
		ok_page("Check-In เรียบร้อย","ระบบทำการจัดเก็บข้อมูลการเข้าพักเรียบร้อยแล้ว",$ok_text,"admin.php","");
		mysql_close($conn);
	//}
}
?>